package de.koller.reason.render.ray;

import de.koller.oceanFramework.math.RVec3;
import de.koller.reason.render.obj.Polygon;

public class Interpolator {

	public Interpolator() {
	
	}

	public float interpolate( Polygon p, RVec3 at, float v0, float v1, float v2 ) {
		float max = Math.max( Math.abs( v0 ), Math.abs( v1 ) );
		max =		Math.max(            max, Math.abs( v2 ) );
		
		RVec3 polygonN = RVec3.cross( RVec3.difference( p.verteces[1].position, p.verteces[0].position ), 
									  RVec3.difference( p.verteces[2].position, p.verteces[0].position ) );
		polygonN.normalize();
		
		RVec3 planeA = p.verteces[0].position.newInstance().add( polygonN.newInstance().mult( v0 ) );
		RVec3 planeB = p.verteces[1].position.newInstance().add( polygonN.newInstance().mult( v1 ) );
		RVec3 planeC = p.verteces[2].position.newInstance().add( polygonN.newInstance().mult( v2 ) );
		
		RVec3 planeN = RVec3.cross( RVec3.difference( planeB, planeA ), 
	   							    RVec3.difference( planeC, planeA ) );
		
		RVec3 rayAt = at.newInstance().add( polygonN.newInstance().mult( max ) );
		
		return Intersectioner.intersection( at, rayAt, planeA, planeN ) * max;
	}
	
}
